package br.com.fafica.sisape.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.fafica.sisape.conexao.ConnectionFactory;
import br.com.fafica.sisape.model.Endereco;
import br.com.fafica.sisape.model.Hospital;

public class DAOHospital{

	public void createHospital(Hospital hospital) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "insert into Hospital "
				+ "(Nome, Cnpj, Rua, Cidade, Telefone) "
				+ "values (?,?,?,?,?)";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, hospital.getNome());
			stmp.setString(2, hospital.getCnpj());
			stmp.setString(3, hospital.getEndereco().getRua());
			stmp.setString(4, hospital.getEndereco().getCidade());
			stmp.setString(5, hospital.getTelefone());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

	public ArrayList<Hospital> readHospital() throws SQLException {
		Connection con = new ConnectionFactory().getConnection();

		String sql = "Select * from Hospital";

		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			ArrayList<Hospital> lista = new ArrayList<Hospital>();
			while (rs.next()) {
				Hospital hospital = new Hospital();
				Endereco endereco = new Endereco();
				hospital.setCod(rs.getInt("Cod"));
				hospital.setNome(rs.getString("Nome"));
				hospital.setCnpj(rs.getString("Cnpj"));
				endereco.setRua(rs.getString("Rua"));
				endereco.setCidade(rs.getString("Cidade"));
				hospital.setEndereco(endereco);
				hospital.setTelefone(rs.getString("Telefone"));
				lista.add(hospital);
			}
			rs.close();
			stmt.close();
			return lista;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
		return null;
	}

	public void updateHospital(Hospital hospital) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "update Hospital set Nome=?"+" , Cnpj=?"
				+" ,Rua=?"
				+ " ,Cidade=?"
				+ " ,Telefone=?"
				+ "where Cod=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, hospital.getNome());
			stmp.setString(2, hospital.getCnpj());
			stmp.setString(3, hospital.getEndereco().getRua());
			stmp.setString(4, hospital.getEndereco().getCidade());
			stmp.setString(5, hospital.getTelefone());
			stmp.setInt(6, hospital.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
	}

	public void deleteHospital(Hospital h) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "delete from Hospital where Cod=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, h.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Deletado com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}
}
